package ps.client.gui.timerwindow;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;


@SuppressWarnings("serial")
public class TimerBar extends JComponent {
    
    public static final Color BAR_COLOR_1 = Color.GREEN;
    public static final Color BAR_COLOR_2 = Color.YELLOW;
    public static final Color BAR_COLOR_3 = Color.RED;
    
    int initialTime = 40;
    int timeLeft = 40;
    int timerWarning = 8;
    
    private float stretch = 1f;
    
    Color currentBarColor = BAR_COLOR_1;
    
    @Override
    public void paint(Graphics g) {
        if (g != null) {
            Graphics2D g2d = (Graphics2D)g;
            
            g2d.setColor(Color.BLACK);
            g2d.fillRect(0, 0, getWidth() - 1, getHeight() - 1);
            
            float stretchedWidth = getWidth() - 3;
            stretchedWidth *= stretch;
            float width = stretchedWidth;
            width /= initialTime;
            width *= timeLeft;
            
            g2d.setColor(Color.DARK_GRAY);
            g2d.fillRect(1, 1, (int)stretchedWidth, getHeight() - 3);
            
            g2d.setColor(currentBarColor);
            g2d.fillRect(1, 1, (int)(width), getHeight() - 3);
            
            g2d.setColor(Color.WHITE);
            g2d.drawRect(1, 1, (int)stretchedWidth, getHeight() - 3);
        }
    }
    
    public Color getBarColor() {
        return BAR_COLOR_1;
    }
    
    public int getInitialTime() {
        return initialTime;
    }

    
    public void setInitialTime(int initialTime) {
        this.initialTime = initialTime;
        repaint();
    }

    public float getStretch() {
        return stretch;
    }

    
    public void setStretch(float stretch) {
        this.stretch = stretch;
        repaint();
    }

    
    public int getTimeLeft() {
        return timeLeft;
    }

    
    public void setTimeLeft(int timeLeft) {
        this.timeLeft = timeLeft > initialTime ? initialTime : timeLeft < 0 ? 0 : timeLeft;
        currentBarColor = timeLeft > 12 ? BAR_COLOR_1 : timeLeft > timerWarning ? BAR_COLOR_2 : BAR_COLOR_3;
        repaint();
    }
    
    public Color getCurrentBarColor() {
        return currentBarColor;
    }
    
    public void setTimerWarning(int timerWarning) {
        this.timerWarning = timerWarning;
    }

}
